﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is Jutsu Opensource Framework.
  
  The Initial Developer of the Original Code is 
  ALCARAZ Marc <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2012-2013
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
  Alternatively, the contents of this file may be used under the terms of
  either the GNU General Public License Version 2 or later (the "GPL"), or
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  in which case the provisions of the GPL or the LGPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
  under the terms of either the GPL or the LGPL, and not to allow others to
  use your version of this file under the terms of the MPL, indicate your
  decision by deleting the provisions above and replace them with the notice
  and other provisions required by the LGPL or the GPL. If you do not delete
  the provisions above, a recipient may use your version of this file under
  the terms of any one of the MPL, the GPL or the LGPL.
  
*/

package jutsu.process.tests
{
    import jutsu.logging.logger;

    import system.process.Task;

    import flash.display.DisplayObjectContainer;
    import flash.display.Stage;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.LocationChangeEvent;
    import flash.geom.Rectangle;
    import flash.media.StageWebView;

    /**
     * This task test the StageWebView class.
     */
    public class StageWebViewTest extends Task
    {
        /**
         * Creates a new StageWebViewTest instance.
         */
        public function StageWebViewTest(target:DisplayObjectContainer = null, stage:Stage = null )
        {
            this.target = target ;
            this.stage  = stage  ;
        }
        
        /**
         * The stage reference.
         */
        public var stage:Stage ;
        
        /**
         * The owner target of this task.
         */
        public var target:DisplayObjectContainer ;
        
        /**
         * The StageWebView reference.
         */
        protected var view:StageWebView = new StageWebView() ;
        
        /**
         * Run the process.
         */
        public override function run( ...arguments:Array ):void
        {
            notifyStarted() ;
            
            if( !view.stage && stage )
            {
                try
                {
                    view.stage = stage ;
                    view.viewPort = new Rectangle( 0 , 0 , stage.fullScreenWidth , stage.fullScreenHeight ) ;
                    registerView() ;
                    
                    view.loadURL("https://maps.google.fr/") ;
                }
                catch( e:Error )
                {
                    logger.error( this + " run failed, " + e.message ) ;
                }
            }
            else
            {
                unregisteView() ;
                view.dispose() ;
                view.viewPort = null ;
                view.stage    = null ;
            }
            
            notifyFinished() ;
        }
        
        ////////////
        
        protected function registerView():void
        {
            if ( view )
            {
                view.addEventListener( Event.COMPLETE                        , complete         );
                view.addEventListener( ErrorEvent.ERROR                      , error            );
                view.addEventListener( LocationChangeEvent.LOCATION_CHANGE   , locationChange   );
                view.addEventListener( LocationChangeEvent.LOCATION_CHANGING , locationChanging );
            }
        }
        
        
        protected function unregisteView():void
        {
            if ( view )
            {
                view.removeEventListener( Event.COMPLETE                        , complete         );
                view.removeEventListener( ErrorEvent.ERROR                      , error            );
                view.removeEventListener( LocationChangeEvent.LOCATION_CHANGE   , locationChange   );
                view.removeEventListener( LocationChangeEvent.LOCATION_CHANGING , locationChanging );
            }
        }
        
        ////////////
        
        /**
         * Signals that an error has occurred.
         */
        private function complete( event:Event ):void
        {
            logger.info( this + " complete location:" + view.location ) ;
        }
        
        
        /**
         * Signals that an error has occurred.
         */
        private function error( event:ErrorEvent ):void
        {
            logger.info( this + " error location:" + event.text ) ;
        }
        
        /**
         * Signals that the location property of the StageWebView object has changed.
         */
        private function locationChange( event:LocationChangeEvent ):void
        {
            logger.info( this + " locationChange location:" + event.location ) ;
        }
        
        /**
         * Only dispatched when the location change is initiated through HTML content or code running inside the StageWebView object, such as when a user clicks a link
         */
        private function locationChanging( event:LocationChangeEvent ):void
        {
            logger.info( this + " locationChanging location:" + event.location ) ;
            event.preventDefault() ;
        }
    }
}
